﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using YiSha.Admin.Web.Controllers;
using YiSha.Business.OrganizationManage;
using YiSha.Business.RedBookSetManage;
using YiSha.Business.SystemManage;
using YiSha.Entity.OrganizationManage;
using YiSha.Entity.RedBookSetManage;
using YiSha.Entity.SystemManage;
using YiSha.Model.Param;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Model.Param.RedBookSetManage;
using YiSha.Model.Result;
using YiSha.Model.Result.SystemManage;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Web.Code;
namespace YiSha.Admin.Web.Areas.RedBookSetManage.Controllers
{
    [Area("RedBookSetManage")]
    public class RedBookSetController : BaseController
    {
        private RedBookSetBll redBookSetBLL = new RedBookSetBll();
        #region 视图功能
        [AuthorizeFilter("redbookset:redbookset:view")]
        public IActionResult RedBookExpandSetIndex()
        {
            return View();
        }
        public IActionResult RedBookSetForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("redbookset:redbookset:search")]
        public async Task<IActionResult> GetPageListJson(RedBookSetListParam param, Pagination pagination)
        {
            TData<List<RedBookSetEntity>> obj = await redBookSetBLL.GetPageList(param, pagination);

            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("redbookset:redbookset:view")]
        public async Task<IActionResult> GetFormJson(long id)
        {
            TData<RedBookSetEntity> obj = await redBookSetBLL.GetEntity(id);
            List<ImageInfo> imageInfos = new List<ImageInfo>();
            if (!string.IsNullOrEmpty(obj.Data.Images))
            {
                foreach (var item in obj.Data.Images.Split(','))
                {
                    string absoluteDir =GlobalContext.HostingEnvironment.ContentRootPath+item.Replace('/','\\');
                    if (System.IO.File.Exists(absoluteDir))
                    {
                        ImageInfo imageInfo = new ImageInfo();
                        imageInfo.type = item.Split('.')[1];
                        imageInfo.caption = item.Split('/').Last();
                        imageInfo.url = GlobalContext.SystemConfig.ApiSite + item;
                        imageInfo.path = item;
                        System.IO.FileInfo fs = new System.IO.FileInfo(absoluteDir);

                        imageInfo.size = fs.Length.ToString();
                        imageInfos.Add(imageInfo);
                    }
                }

            }

            obj.Description= JsonConvert.SerializeObject(imageInfos);
            return Json(obj);
        }
        [HttpGet]
        [AuthorizeFilter("redbookset:redbookset:view")]
        public async Task<IActionResult> GetFormAreaJson()
        {
            TData<List<AreaEntity>> obj = await new YiSha.Business.SystemManage.AreaBLL().GetList(new Model.Param.SystemManage.AreaListParam());
            obj.Data = obj.Data.Where(x => x.AreaLevel < 3).ToList();
            TData<List<ZtreeInfo>> ztreeInfo = new TData<List<ZtreeInfo>>();
            ztreeInfo.Data = new List<ZtreeInfo>(); ;
            foreach (AreaEntity area in obj.Data)
            {
                ztreeInfo.Data.Add(new ZtreeInfo
                {
                    id = area.AreaCode.ParseToLong(),
                    pId = area.ParentAreaCode.ParseToLong(),
                    name = area.AreaName
                });
            }
            return Json(ztreeInfo);
        }

        [HttpGet]
        public async Task<IActionResult> GetMaxSortJson()
        {
            TData<int> obj = await redBookSetBLL.GetMaxSort();
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("redbookset:redbookset:add,redbookset:redbookset:edit")]
        public async Task<IActionResult> SaveFormJson(RedBookSetEntity entity)
        {
            var obj = new TData<string>();
            if (!string.IsNullOrEmpty(entity.Images))
            {
                var imagesArray = entity.Images.Split(',');
                if (imagesArray.Length > 5)
                {
                    obj.Tag = 0;
                    obj.Message = "图片超过5张";
                    return Json(obj);
                }
            }

            OperatorInfo user = await Operator.Instance.Current();
            entity.UserId = user.UserId;
            obj = await redBookSetBLL.SaveForm(entity);
            obj.Tag = 1;
            return Json(obj);
        }
        [HttpPost]
        [AuthorizeFilter("redbookset:redbookset:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TData obj = await redBookSetBLL.DeleteForm(ids);
            return Json(obj);
        }


        #endregion


    }

    public class ImageInfo
    {
        public string type { get; set; }
        public string caption { get; set; }
        public string path { get; set; }
        public string size { get; set; }
        public string url { get; set; }


    }
}
